#include "ipc.h"

int main(int argc,char** argv){

    char* data;
    /*make the key*/
    SharedMemory<4096,IPC_KEY_1>::makeKey();    
    /*connect to the segment*/
    SharedMemory<4096,IPC_KEY_1>::connectToSegment();
    /*attach to the segment to get a pointer*/
    data = SharedMemory<4096,IPC_KEY_1>::getPointerToShmem();
   
    if(data == (char*) (-1)){
        perror("shmat");
        exit(1);
    }
    
    for(int i=0;i<100;i++){     
        /*PROCESSUS ECRIVAIN*/
        /*read and modify the segment*/
        if(argc==2){
               char text[10];
               sprintf(text,"step%d\n",i);
               printf("writing to the segment: %s \n",text);
               strncpy(data,text,4096);
               sleep(10);

               
        }else{
           /*PROCESSUS LECTEUR*/
           sleep(2);
           printf("segment contains : %s\n",data); 
        }
    }
    
    /*detach from the segment*/
    SharedMemory<4096,IPC_KEY_1>::detachSegment();
}
